public class Individual {

    static int defaultGeneLength = 20;
    private byte[] genes = new byte[defaultGeneLength];
    // Cache
    private double fitness = 0;

    // Create a random individual
    public void generateIndividual() {
        for (int i = 0; i < size(); i++) {
            byte gene = (byte) Math.round(Math.random());
            genes[i] = gene;
        }
    }

    /* Getters and setters */
    // Use this if you want to create individuals with different gene lengths
    public static void setDefaultGeneLength(int length) {
        defaultGeneLength = length;
    }
    
    public byte getGene(int index) {
        return genes[index];
    }

    public void setGene(int index, byte value) {
        genes[index] = value;
        fitness = 0;
    }

    /* Public methods */
    public int size() {
        return genes.length;
    }

    public double getFitness() {
        if (fitness == 0) {
            fitness = FitnessCalc.getFitness(this);
        }
        return fitness;
    }

    @Override
    public String toString() {
        String geneString = "";
        for (int i = 0; i < size(); i++) {
            geneString += getGene(i);
        }
        return geneString;
    }
}
/*
pop variation
2 & 5908.42 & 5900.96\\
4 & 6635.12 & 6457.83\\
8 & 6316.98 & 5341.11\\
16 & 6505.14 & 5738.32\\
32 & 6649.82 & 5443.11\\
64 & 6655.82 & 5684.69\\
128 & 6665.28 & 5603.44\\
256 & 6666.16 & 5750.90\\
512 & 6663.62 & 5608.93\\
1024 & 6663.86 & 5621.22\\


mutation

0.01 & 6525.52 & 5375.47\\
0.11 & 6638.98 & 6082.28\\
0.21 & 6441.5 & 4293.02\\
0.31 & 6524.42 & 5269.20\\
0.41 & 6598.08 & 5448.53\\
0.51 & 6612.58 & 5596.96\\
0.61 & 6603.98 & 5568.89\\
0.71 & 6386.18 & 5686.62\\
0.81 & 6426.54 & 5177.71\\
0.91 & 6288.88 & 4906.46\\

croisement

0.1 & 6463.33 & 6463.33\\
0.2 & 6061.18 & 6059.21\\
0.3 & 6645.76 & 6643.07\\
0.4 & 6666.32 & 6666.31\\
0.5 & 6657.18 & 6652.48\\
0.6 & 6585.42 & 6090.07\\
0.7 & 6470.08 & 6359.53\\
0.8 & 6658.0 & 6658.0\\
0.9 & 6619.92 & 6607.53\\
1.0 & 6585.02 & 6503.19\\

nbIterations

1 & 6630.18 & 5810.36\\
2 & 6554.94 & 6230.97\\
3 & 6636.55 & 6619.00\\
4 & 6516.34 & 6512.42\\
5 & 6588.98 & 6547.17\\
6 & 6638.42 & 6636.48\\
7 & 6573.82 & 6573.81\\
8 & 6625.04 & 6624.75\\
9 & 6527.45 & 6525.64\\
10 & 6647.92 & 6606.29\\
*/